﻿using aier.business.logic;
using aier.dataaccess.model;
using aier.dataaccess.dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using log4net;
using System.Collections;
using System.Web;
using System.IO;

namespace aier.wx.api.selfhost.Controllers
{
  
    [RoutePrefix("api/Appointment")]
    public class AppointmentController : ApiController
    {
        public AppointmentBll appointmentBll { get; set; }

        public CommonBll commonBll { get; set; }

        public ILog log { get; set; }


        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }


        /// <summary>
        /// 根据计划拜访开始,结束时间查询拜访信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAppointmentByTime")]
        public IHttpActionResult GetAppointmentByTime(string startDate,string endDate)
        {
           
            IList appointmentList = appointmentBll.QueryAppointmentByTime(startDate,endDate).ToList();
            return Ok(appointmentList);
        }

        /// <summary>
        /// 根据id查询拜访信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAppointmentById")]
        public IHttpActionResult GetAppointmentById(Guid activityId)
        {
            AppointmentView appointment = appointmentBll.QryAppointment(activityId);
            return Ok(appointment);
        }

        /// <summary>
        /// 根据计划拜访开始,结束时间,状态查询拜访信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetAppointmentByState")]
        public IHttpActionResult GetAppointmentByState(int statecode,string startDate, string endDate,int page,int pageSize)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            Guid userid = new Guid(userInfo.userid.ToString());
            PageResult<AppointmentView> pageResult = appointmentBll.QryAppointmentList(Guid.Empty,statecode,startDate, endDate, userid, page, pageSize);
            return Ok(pageResult);
        }


        /// <summary>
        /// 分页查询拜访信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetAppointmentByPage")]
        public IHttpActionResult GetAppointmentByPage(int page,int pageSize,string sort, string starttime, string endtime)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            Guid userid = new Guid(userInfo.userid.ToString());
            List<AppointmentView> list = appointmentBll.QryAppointmentListPage(page, pageSize, sort, userid, starttime, endtime).ToList();
             return Ok(list);
            //return Ok(appointmentBll.QryAppointmentListPage(1, 20, "desc", userid));
        }

        /// <summary>
        /// 根据拜访对象分页查询拜访信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetAppointmentByContactId")]
        public IHttpActionResult GetAppointmentByContactId(int page, int pageSize, int? statecode,string contactid,string starttime, string endtime,string districtid)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            Guid userid = new Guid(userInfo.userid.ToString());
            Guid cguid = Guid.Empty;
            Guid dguid = Guid.Empty;
            if (!string.IsNullOrWhiteSpace(contactid)) {
                cguid = new Guid(contactid);
            }
            if (!string.IsNullOrWhiteSpace(districtid)) {
                dguid = new Guid(districtid);
            }
            PageResult<AppointmentView> pageResult = appointmentBll.QryAppointmentListV2(Guid.Empty, statecode, starttime, endtime, userid, cguid, page, pageSize);
            return Ok(pageResult);
            //return Ok(appointmentBll.QryAppointmentListPage(1, 20, "desc", userid));
        }


        /// <summary>
        /// 添加拜访信息
        /// </summary>
        /// <param name="appointmenMolde"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("AddAppointment")]
        public IHttpActionResult AddAppointment(AppointmenMolde appointmenMolde)
        {        
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                appointmenMolde.userid = userInfo.userid;
                //appointmenMolde.new_visitahospital = userInfo.siteid;
                if (appointmenMolde != null)
                {
                    ResResult resResult = appointmentBll.AddAppointment(appointmenMolde);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 修改拜访信息
        /// </summary>
        /// <param name="appointmenMolde"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("ModAppointment")]
        public IHttpActionResult ModAppointment(AppointmenMolde appointmenMolde)
        {         
            try
            {
                if (appointmenMolde !=null)
                {
                    ResResult resResult = appointmentBll.ModAppointment(appointmenMolde);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        /// <summary>
        /// 修改状态
        /// 对应关系如下
        /// stateCode =0,statusCode=1 开启
        /// stateCode=1,statusCode=3  已完成
        /// stateCode=2,statusCode=4 取消
        /// stateCode=3,statusCode=5 进行中
        /// </summary>
        /// <param name="activityid"></param>
        /// <param name="statusCode"></param>
        /// <param name="stateCode"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("UpdateStatus")]
        public IHttpActionResult UpdateStatus(Guid activityid, int stateCode, int statusCode)
        {
            try
            {
                if (activityid != Guid.Empty)
                {
                    ResResult resResult = appointmentBll.UpdateStatus(activityid, stateCode, statusCode);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
           
        }

       /// <summary>
       /// 删除拜访信息
       /// </summary>
       /// <param name="activityid"></param>
       /// <returns></returns>
        [HttpPost]
        [ActionName("DelAppointment")]
        public IHttpActionResult DelAppointment(Guid activityid)
        {
            try
            {
                if (activityid!=Guid.Empty) {
                    ResResult resResult = appointmentBll.DelAppointment(activityid);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
           
        }

        /// <summary>
        /// 完成
        /// </summary>
        /// <param name="appointmenMolde"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("AppointmentCompelete")]
        public IHttpActionResult AppointmentCompelete(AppointmenMolde appointmenMolde)
        {
            try
            {
                if (appointmenMolde != null)
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    appointmenMolde.userid = userInfo.userid;
                    ResResult resResult = commonBll.AppointmentCheckInOrCompelete(appointmenMolde, 1, 3);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }


        /// <summary>
        /// 签到
        /// </summary>
        /// <param name="appointmenMolde"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ActionName("AppointmentCheckIn")]
        public IHttpActionResult AppointmentCheckIn(AppointmenMolde appointmenMolde) {
            try
            {
                if (appointmenMolde != null)
                {
                    UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                    appointmenMolde.userid = userInfo.userid;
                    ResResult resResult = commonBll.AppointmentCheckInOrCompelete(appointmenMolde,3,5);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }



    }
}
